package com.xhxg.dao.impl;

import com.xhxg.dao.CustomerDao;
import com.xhxg.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

//spring dao接口实现类
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public Customer selectCustomer(int id) {
        String sql = "select * from t_customer where id = ?";

        System.out.println("CustomerDaoImpl调用DAO查询语句...");

        return this.jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Customer.class), id);
    }

    @Override
    public int insertCustomer(Customer customer) {
        String sql = "insert into t_customer(username,jobs,phone) value(?,?,?)";
        Object[] obj = new Object[]{customer.getUsername(),
                customer.getJobs(), customer.getPhone()};

        return this.jdbcTemplate.update(sql, obj);
    }

    @Override
    public int updateCustomerXML(Customer customer) {
        String sql = "update t_customer set username=?,jobs=? where id = ?";
        Object[] params = new Object[]{customer.getUsername(), customer.getJobs(), customer.getId()};

        int num = this.jdbcTemplate.update(sql, params);
        int i = 1 / 0;

        return num;
    }

    @Override
    public int updateCustomerAnnon(Customer customer) {
        String sql = "update t_customer set username=?,jobs=? where id = ?";
        Object[] params = new Object[]{customer.getUsername(), customer.getJobs(), customer.getId()};
        return this.jdbcTemplate.update(sql, params);
    }

    @Override
    public int deleteCustomer(int id) {
        String sql = "delete from t_customer where id = ? ";
        return this.jdbcTemplate.update(sql, id);
    }
}